//package com.coldchain.task;
//
//import java.util.Date;
//import java.util.List;
//import java.util.concurrent.TimeUnit;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.coldchain.admin.client.feign.CompanyFeign;
//import com.coldchain.order.entity.BizDispatchingOrdercao;
//import com.coldchain.order.entity.BizReceiverGoods;
//import com.coldchain.order.entity.BizReceiverPerson;
//import com.coldchain.order.entity.CreditOrder;
//import com.coldchain.order.entity.Order;
//import com.coldchain.order.entity.PlanOrder;
//import com.coldchain.order.mapper.CreditOrderMapper;
//import com.coldchain.order.service.IBizDispatchingOrderServicecoo;
//import com.coldchain.order.service.IBizReceiverGoodsService;
//import com.coldchain.order.service.IBizReceiverPersonService;
//import com.coldchain.order.service.IOrderService;
//import com.coldchain.order.service.IPlanOrderService;
//import com.fangliai.common.exception.base.BusinessException;
//import com.fangliai.common.msg.ObjectRestResponse;
//import com.fangliai.common.util.DateUtil;
//import com.fangliai.common.util.FunctionUtils;
//import com.fangliai.common.util.ValidateUtil;
//
//@Component
//public class CreditOrderServiceTask {
//	@Autowired
//	private CreditOrderMapper creditOrderMapper;
//	@Autowired
//	private IPlanOrderService planOrderService;
//	@Autowired
//	private IOrderService orderService;
//	@Autowired
//	private IBizDispatchingOrderServicecoo bizDispatchingOrderService;
//	@Autowired
//	private IBizReceiverPersonService bizReceiverPersonService;
//	@Autowired
//	private IBizReceiverGoodsService bizReceiverGoodsService;
//	@Autowired
//	private CompanyFeign companyFeign;
//	@Scheduled(cron="0 0 0 * * ?")
//	public void orderTask(){
//		List<PlanOrder> list = this.planOrderService.list(new QueryWrapper<PlanOrder>());
//		for(PlanOrder p:list){
//			try{
//				switch (p.getPlanType()) {
//				case 1:{
//					this.handleOrder(p);
//					break;
//				}
//				case 2:{
//					String day = DateUtil.getWeekOfDate(new Date());
//					String[] days = p.getDay().split(",");
//					for(String str:days){
//						if(ValidateUtil.stringSame(day, str)){
//							this.handleOrder(p);
//						}
//					}
//					break;
//				}
//				case 3:{
//					 Date date = new Date();
//					 int day = date.getDate();
//					 String[] days = p.getDay().split(",");
//						for(String str:days){
//							if(ValidateUtil.stringSame(day+"", str)){
//								this.handleOrder(p);
//							}
//						}
//					break;
//				}
//				default:
//					break;
//				}
//			}catch(Exception e){
//				e.printStackTrace();
//				System.out.println("############"+e.getMessage());
//			}
//		}
//	}
//	public void handleOrder(PlanOrder p){
//		Order order = this.orderService.getById(p.getOrderId());
//		order.setOrderId(FunctionUtils.getOrderCode("",""));
//		order.setTime(new Date());
//		order.setStatus(1);
//		order.setSettlementCode(0);
//		order.setChannel(4);
//		order.setTransactionId("");
//		order.setRefundId("");
//		order.setErrCode("");
//		order.setErrCodeDes("");
//		order.setResultCode("SUCCESS");
//		order.setSettlementTime(null);
//		order.setVersion(0);
//		ObjectRestResponse<String> useAmountPayOrder = companyFeign.useAmountPayOrder(order.getCompanyId(),order.getOrderId(),order.getPaymoney());
//		if(useAmountPayOrder.getStatus()!=200){
//			throw new BusinessException(useAmountPayOrder.getMessage());
//		}
//		BizDispatchingOrdercao bizorder = this.bizDispatchingOrderService.getById(p.getBizId());
//		bizorder.setOrderId(order.getOrderId());
//		bizorder.setBizId(FunctionUtils.getOrderCode("",""));
//		bizorder.setRuntime(new Date());
//		bizorder.setCreatetime(new Date());
//		bizorder.setStatus(1);
//		bizorder.setStartLogUrl("");
//		bizorder.setErrorLogUrl("");
//		bizorder.setTurnid(null);
//		bizorder.setErrorCode(null);
//		bizorder.setCarId(null);
//
//		QueryWrapper<BizReceiverPerson> queryWrapper = new QueryWrapper<BizReceiverPerson>();
//		queryWrapper.eq("biz_id", p.getBizId());
//		List<BizReceiverPerson> receivers = this.bizReceiverPersonService.list(queryWrapper);
//		for(BizReceiverPerson receiver:receivers){
//			receiver.setStatus(0);
//			receiver.setCarrierNum(0);
//			receiver.setCreatetime(new Date());
//			receiver.setGoodsLogUrl("");
//			receiver.setResultLogUrl("");
//			receiver.setErrorMsg("");
//			receiver.setErrorCode(null);
//			receiver.setErrorLogUrl("");
//			receiver.setEndLogUrl("");
//		}
//		QueryWrapper<BizReceiverGoods> goods = new QueryWrapper<BizReceiverGoods>();
//		goods.eq("biz_id", p.getBizId());
//		List<BizReceiverGoods> allGoods = bizReceiverGoodsService.list(goods);
//		for(BizReceiverGoods good:allGoods){
//			good.setBizId(bizorder.getBizId());
//		}
//		boolean falg = false;
//		if(this.orderService.save(order)){
//			if(this.bizDispatchingOrderService.save(bizorder)){
//				if(this.bizReceiverPersonService.saveBatch(receivers)){
//					if(this.bizReceiverGoodsService.saveBatch(allGoods)){
//						falg = true;
//					}
//				}
//			}
//		}
//		if(falg){
//			//结算订单
//			CreditOrder creditOrder = this.creditOrderMapper.getUserId(order.getCompanyId(),DateUtil.getStartTimeOfMonth(new Date()));
//			if(creditOrder!=null){
//				creditOrder.setOrderCount(creditOrder.getOrderCount()+1);
//				creditOrder.setPrice(FunctionUtils.add(creditOrder.getPrice(),order.getPaymoney(),2));
//				this.creditOrderMapper.updateById(creditOrder);
//			}else{
//				creditOrder = new CreditOrder();
//				creditOrder.setUserId(order.getCompanyId());
//				creditOrder.setPrice(order.getPaymoney());
//				creditOrder.setOrderCount(1);
//				this.creditOrderMapper.insert(creditOrder);
//			}
//		}
//	}
//	@Scheduled(cron="* * * 1 * ?")
//	public void orderMessagePush(){
//		QueryWrapper<CreditOrder> wrapper = new QueryWrapper<CreditOrder>();
//		wrapper.between("createtime",DateUtil.getStartTimeOfMonth(new Date())
//				,DateUtil.getEndTimeOfMonth(new Date()));
//		Integer count = this.creditOrderMapper.selectCount(wrapper);
//		if(count==null){
//			count =1;
//		}
//		int pagesize=1000;
//		int pages = (pagesize+count-1)/pagesize;
//		for(int page = 1; page < pages+1;page++){
//			Page<CreditOrder> p = new Page<CreditOrder>();
//			p.setCurrent(page);
//			p.setSize(pagesize);
//			IPage<CreditOrder> quert = this.creditOrderMapper.quert(p,wrapper);
//			List<CreditOrder> list = quert.getRecords();
//			for(CreditOrder creditOrder:list){
//				//XXX 消息推送
//			}
//		}
//
//	}
//}
